<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="专家组" prop="expertGroupName">
        <el-input
          v-model="queryParams.expertGroupName"
          placeholder="请输入专家组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="projectDepartment">
        <el-input
          v-model="queryParams.projectDepartment"
          placeholder="请输入项目所属部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:expertcustomgroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:expertcustomgroup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:expertcustomgroup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:expertcustomgroup:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expertcustomgroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="ID" width="55" align="left" prop="id" />
      <el-table-column label="专家组名称" align="left" prop="expertGroupName" />
      <el-table-column label="项目所属部门" align="left" prop="projectDepartment" />
      <el-table-column label="专家数量" align="left" prop="expertNum" />
      <el-table-column label="备注" align="left" prop="remark" />
      <el-table-column label="添加日期" align="left" prop="createTime" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-add"
            @click="setExpertCustom(scope.row)"
            v-hasPermi="['system:expertcustomgroup:edit']"
          >设置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:expertcustomgroup:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:expertcustomgroup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自定义专家库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="专家组名称" prop="expertGroupName">
          <el-input v-model="form.expertGroupName" placeholder="请输入专家组名称" />
        </el-form-item>
        <el-form-item label="项目所属部门" prop="projectDepartment">
          <treeselect @select="selectProjectDept" v-model="form.projectDepartmentId" :options="deptList" :show-count="true" placeholder="请选择归属部门" style="width: 280px"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 自定义专家列表对话框 -->
    <el-dialog :title="expertCustomTitle" :visible.sync="openExpertCustom" :width="customWidth" append-to-body>
      <!-- 搜索工具 -->
      <el-form :model="queryCustomParams" ref="queryCustomForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="专家姓名" prop="expertName">
          <el-input
            v-model="queryCustomParams.params['expertName']"
            placeholder="请输入姓名"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleCustomQuery"
          />
        </el-form-item>

        <el-form-item label="证件号码" prop="identityCode">
          <el-input
            v-model="queryCustomParams.params['identityCode']"
            placeholder="请输入证件号码"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleCustomQuery"
          />
        </el-form-item>

        <el-form-item label="手机号码" prop="mobile">
          <el-input
            v-model="queryCustomParams.params['mobile']"
            placeholder="请输入手机号码"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleCustomQuery"
          />
        </el-form-item>

        <el-form-item label="地区" prop="areaId">
          <el-select v-model="queryCustomParams.params['areaId']" style="width: 120px;" clearable placeholder="请选择">
              <el-option
                v-for="item in companyareaList"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="专家类型" prop="expertTypeId">
          <el-select v-model="queryCustomParams.params['expertTypeId']" style="width: 120px;" clearable placeholder="请选择">
              <el-option
                v-for="item in experttypeList"
                :key="item.id"
                :label="item.text"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleCustomQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetCustomQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleCustomExpertAdd"
            v-hasPermi="['system:expert:add']"
          >批量增加</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multipleCustomExpert"
            @click="handleCustomExpertDelete"
            v-hasPermi="['system:expert:remove']"
          >移除专家</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExpertCustomExport"
            v-hasPermi="['system:expert:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="expertcustomgroupexpertList" :height="customHeight" @selection-change="handleSelectionCustomGroupExpertChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="id" />
          <el-table-column label="专家姓名" align="center" prop="expert.expertName" />
          <el-table-column label="证件号码" align="center" prop="expert.identityCode" />
          <el-table-column label="手机号码" align="center" prop="expert.mobile" />
          <el-table-column label="专家品目" align="center" prop="expert.expertCategoryName" />
          <el-table-column label="地区" align="center" prop="expert.areaName" />
          <el-table-column label="专家类型" align="center" prop="expert.expertTypeName" />
        </el-table>

        <pagination
          v-show="customgroupexpertTotal>0"
          :total="customgroupexpertTotal"
          :page.sync="queryExpertParams.pageNum"
          :limit.sync="queryExpertParams.pageSize"
          @pagination="getExpertCustomExpertList"
        />
      </el-dialog>

    </el-dialog>

    <!-- 专家列表对话框 -->
    <el-dialog :title="expertTitle" :visible.sync="openExpert" :width="customWidth" append-to-body>
      <el-form :model="queryExpertParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="专家姓名" prop="expertName">
          <el-input
            v-model="queryExpertParams.expertName"
            placeholder="请输入姓名"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleExpertQuery"
          />
        </el-form-item>

        <el-form-item label="证件号码" prop="identityCode">
          <el-input
            v-model="queryExpertParams.identityCode"
            placeholder="请输入证件号码"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleExpertQuery"
          />
        </el-form-item>

        <el-form-item label="手机号码" prop="mobile">
          <el-input
            v-model="queryExpertParams.mobile"
            placeholder="请输入手机号码"
            clearable
            style="width: 150px;"
            @keyup.enter.native="handleExpertQuery"
          />
        </el-form-item>

        <el-form-item label="地区" prop="areaId">
          <el-select v-model="queryExpertParams.areaId" style="width: 120px;" clearable placeholder="请选择">
              <el-option
                v-for="item in companyareaList"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="专家类型" prop="expertTypeId">
          <el-select v-model="queryExpertParams.expertTypeId" style="width: 120px;" clearable placeholder="请选择">
              <el-option
                v-for="item in experttypeList"
                :key="item.id"
                :label="item.text"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleExpertQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetExpertQuery">重置</el-button>
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="singleExpert"
            @click="batchCustomExpertAdd"
          >批量添加</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="expertList" :height="expertHeight" @selection-change="handleSelectionExpertChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="left" width="55" prop="id" />
        <el-table-column label="专家姓名" align="left"  width="100" prop="expertName" />
        <el-table-column label="证件号码" align="left" prop="identityCode" />
        <el-table-column label="手机号码" align="left" prop="mobile" />
        <el-table-column label="专家品目" align="left" prop="expertCategoryName" />
        <el-table-column label="地区" align="left" prop="areaName" />
        <el-table-column label="专家类型" align="left" prop="expertTypeName" />
      </el-table>

      <pagination
        v-show="expertTotal>0"
        :total="expertTotal"
        :page.sync="queryExpertParams.pageNum"
        :limit.sync="queryExpertParams.pageSize"
        @pagination="getExpertList"
      />
    </el-dialog>
  </div>
</template>

<script>

import { listExpertcustomgroup, getExpertcustomgroup, delExpertcustomgroup, addExpertcustomgroup, updateExpertcustomgroup } from "@/api/system/expertcustomgroup";
import { listExpertcustomgroupexpert, getExpertcustomgroupexpert, delExpertcustomgroupexpert, addExpertcustomgroupexpert, updateExpertcustomgroupexpert,batchaddExpertcustomgroupexpert } from "@/api/system/expertcustomgroupexpert";
import { listExpert, getExpert, delExpert, addExpert, updateExpert } from "@/api/system/expert";
import { listsIdentitytype } from "@/api/system/identitytype";
import { listsCompanyarea } from "@/api/system/companyarea";
import { listsExpertcategory } from "@/api/system/expertcategory";
import { listsType } from "@/api/system/experttype";
import { listsCompanytype } from "@/api/system/companytype";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Expertcustomgroup",
  components: {Treeselect },
  data() {
    return {
      customGroupId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      customExpertIds: [],
      // 非单个禁用
      single: true,
      singleExpert: true,
      // 非多个禁用
      multiple: true,
      multipleCustomExpert: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      customgroupexpertTotal: 0,
      expertTotal: 0,
      // 自定义专家库列表
      expertcustomgroupList: [],
      // 专家列表
      expertList: [],
      // 自定义专家列表
      expertcustomgroupexpertList: [],
      // 部门列表
      deptList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题 设置专家
      expertCustomTitle: "",
      // 计算属性设置宽度
      customWidth: `calc(100% - 500px)`,
      customHeight: '600px',
      expertHeight: '400px',
      // 是否显示弹出层:自定义专家列表
      openExpertCustom: false,
      openExpert: false,
      expertTitle: "",
      // 地区列表
      companyareaList: [],
      // 身份类型列表
      identitytypeList: [],
      // 专家品目列表
      expertcategoryList: [],
      // 专家类型列表
      experttypeList: [],
      // 企业类型列表
      companytypeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expertGroupName: null,
        projectDepartment: null,
        projectDepartmentId: null,
        remark: null,
        params:{}
      },
      // 查询参数
      queryCustomParams: {
        pageNum: 1,
        pageSize: 10,
        expertName: null,
        mobile: null,
        customGroupId: null,
        params:{
          'expertName': null,
          'mobile': null,
          'identityCode': null,
          'expertCategoryName': null,
          'ids':null,
        }
      },
      queryExpertParams: {
        pageNum: 1,
        pageSize: 10,
        expertName: null,
        mobile: null,
        expertName: null,
        mobile: null,
        identityCode: null,
        expertCategoryName: null,
      },
      // 表单参数
      form: {},
      customExpertForm: {
        customGroupId: null,
        expertId: null
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
    this.getCompanyAreaList();
    this.getIdentitytypeList();
    this.getExpertCategoryList();
    this.getExpertTypeList();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptList = response.data;
      });
    },
    /** 查询自定义专家库列表 */
    getList() {
      this.loading = true;
      listExpertcustomgroup(this.queryParams).then(response => {
        this.expertcustomgroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询自定义专家库列表 */
    getExpertCustomExpertList() {
      this.loading = true;
      listExpertcustomgroupexpert(this.queryCustomParams).then(response => {
        this.expertcustomgroupexpertList = response.rows;
        this.customgroupexpertTotal = response.total;
        this.loading = false;
      });
    },
    /** 查询专家列表 */
    getExpertList() {
      this.loading = true;
      listExpert(this.queryExpertParams).then(response => {
        this.expertList = response.rows;
        this.expertTotal = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        expertGroupName: null,
        projectDepartment: null,
        projectDepartmentId: null,
        remark: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    resetCustomExpert(){
      this.customExpertForm = {
        customGroupId: this.customGroupId,
        expertId: null
      };
      this.resetForm("customExpertForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 自定义专家列表 */
    handleCustomQuery() {
      this.queryCustomParams.pageNum = 1;
      console.log(this.queryCustomParams)
      this.getExpertCustomExpertList();
    },
    handleCustomExpertQuery() {
      this.queryCustomParams.pageNum = 1;
      this.getExpertCustomExpertList();
    },
    handleExpertQuery() {
      this.queryExpertParams.pageNum = 1;
      this.getExpertList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    resetCustomQuery() {
      this.resetForm("queryCustomForm");
      this.queryCustomParams =  {
        pageNum: 1,
        pageSize: 10,
        expertName: null,
        mobile: null,
        customGroupId: this.customGroupId,
        params:{
          'expertName': null,
          'mobile': null,
          'identityCode': null,
          'expertCategoryName': null,
        }
      };
      this.handleCustomQuery();
    },
    resetExpertQuery() {
      this.resetForm("queryExpertForm");
      this.handleExpertQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 多选框选中数据
    handleSelectionCustomGroupExpertChange(selection) {
      this.customExpertIds = selection.map(item => item.id)
      this.multipleCustomExpert = !selection.length
    },
    handleSelectionExpertChange(selection) {
      this.singleExpert = !selection.length
      this.customExpertForm = {
        customGroupId: this.customGroupId,
        expertId: selection.map(item => item.id).join(',')
      };
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自定义专家库";
    },
    handleCustomExpertAdd() {
      this.resetCustomExpert();
      this.openExpert = true;
      this.expertTitle = "专家列表";
      // 查询专家列表
      this.getExpertList();
    },
    handleCustomExpertDelete(){
      const ids = this.customExpertIds;
      this.$modal.confirm('是否确认删除自定义专家编号为"' + ids + '"的数据项？').then(function() {
        return delExpertcustomgroupexpert(ids);
      }).then(() => {
        this.getExpertCustomExpertList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExpertcustomgroup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改自定义专家库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExpertcustomgroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExpertcustomgroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除自定义专家库编号为"' + ids + '"的数据项？').then(function() {
        return delExpertcustomgroup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/expertcustomgroup/export', {
        ...this.queryParams
      }, `expertcustomgroup_${new Date().getTime()}.xlsx`)
    },
    handleExpertCustomExport(){
      this.download('system/expertcustomgroupexpert/export', {
        ...this.queryCustomParams
      }, `expertcustomgroupexpert_${new Date().getTime()}.xlsx`)
    },
    selectProjectDept(value) {
      this.form.projectDepartment = value.label;
    },
    setExpertCustom(row){
      // 请求列表
      this.customGroupId = row.id;
      this.queryCustomParams.customGroupId = row.id;
      this.getExpertCustomExpertList();

      // 显示弹窗
      this.expertCustomTitle = "专家设置("+row.expertGroupName+")";
      this.openExpertCustom = true;
    },
    /**获取地区列表**/
    getCompanyAreaList(){
      this.loading = true;
      listsCompanyarea(this.queryParams).then(response => {
        this.companyareaList = response.data;
        this.loading = false;
      });
    },
    /**获取身份类型列表**/
    getIdentitytypeList(){
      this.loading = true;
      listsIdentitytype(this.queryParams).then(response => {
        this.identitytypeList = response.data;
        this.loading = false;
      });
    },
    /**获取专家品目列表**/
    getExpertCategoryList(){
      this.loading = true;
      listsExpertcategory(this.queryParams).then(response => {
        this.expertcategoryList = response.data;
        this.loading = false;
      });
    },
    /**获取专家型列表**/
    getExpertTypeList(){
      this.loading = true;
      listsType(this.queryParams).then(response => {
        this.experttypeList = response.data;
        this.loading = false;
      });
    },
    /**
     * 批量添加专家
     */
    batchCustomExpertAdd(){
      this.loading = true;
      batchaddExpertcustomgroupexpert(this.customExpertForm).then(response => {
        //this.expertList = response.rows;
        //this.total = response.total;
        //this.loading = false;
        this.getExpertCustomExpertList();
        this.openExpert = false;
      });
    },
  }
};
</script>
